<?php

class EmpController extends Controller
{
        public $defaultAction="mgremporg";
	public function actionIndex()
	{
                $criteria=new CDbCriteria();
                $criteria->with=array(
                    'dep',
                );
                $model=Emp::model()->findAll($criteria);
                var_dump($model[0]->dep);
		$this->render('index');
	}
        
        public function actionMgremporg() //mgremporg
        {
                $criteria=new CDbCriteria;
                $criteria->with=array(
                    'emporg',
                );                
		$dataProvider=new CActiveDataProvider('Org',array(
                    'criteria'=>$criteria,
                    'pagination'=>array(
                        'pageSize'=>20,
                    ),                    
                ));
		$this->render('mgremporg',array(
			'dataProvider'=>$dataProvider,
		));
        }
        
        public function actionAddemporg($id){
                $criteria=new CDbCriteria;
                $criteria->condition='t.id=:id';
                $criteria->with=array(
                    'emporg',
                );
                $criteria->params=array(':id'=>$id);
		$dataProvider=new CActiveDataProvider('Org',array(
                    'criteria'=>$criteria,
                    'pagination'=>array(
                        'pageSize'=>20,
                    ),                    
                ));            
		$this->render('addemporg',array(
			'dataProvider'=>$dataProvider,
                        'id_org'=>$id,
		));            
        }
        
        public function actionUpdateemporg(){
            $res=array('result'=>'Данные не получены');
            $res='<div class="alert alert-warning">Данные не получены</div>';
            if (isset($_REQUEST['id_org']) && isset($_REQUEST['id_emp'])) {
                $emporg=new EmpOrg;
                $emporg->id_emp=(int)$_REQUEST['id_emp'];
                $emporg->id_org=(int)$_REQUEST['id_org'];
                if (Emporg::uniqueEmporg($emporg->id_emp,$emporg->id_org)==0) {
                    if ($emporg->save()) $res='<div class="alert alert-success">Успешно добавлен</div>';
                    else $res='<div class="alert alert-danger">Не удалось сохранить</div>';
                } else $res='<div class="alert alert-danger">Выбранный пользователь уже может увольнять в этой организации</div>';
            }
            echo $res;
            Yii::app()->end();
        }
        
        public function actionDelemporg($id_emp=null,$id_org=null) {
            if (is_null($id_emp) || is_null($id_org)) return;
            $model=EmpOrg::model()->deleteByPk(array('id_emp'=>$id_emp,'id_org'=>$id_org));
            $this->redirect($this->createUrl('emp/addemporg',array('id'=>$id_org)));
        }


        public function actionAutocomplete() {

            $res =array();
            if (isset($_GET['term'])) {
                    // http://www.yiiframework.com/doc/guide/database.dao
                    $qtxt ="SELECT name as label, id as value FROM emp WHERE name LIKE :username";
                    $command =Yii::app()->db->createCommand($qtxt);
                    $command->bindValue(":username", '%'.$_GET['term'].'%', PDO::PARAM_STR);
                    $res =$command->queryAll();
                    //$res =$command->queryColumn();
            }

            echo CJSON::encode($res);
            Yii::app()->end();     
        }

        public function actionUpdate()
	{
		$this->render('update');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}